import java.util.stream.IntStream;

/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.06.15
 *Time:10:58
 */
public class Main{

    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("abc");
        //追加
        stringBuilder.append("cde");//abcdef
        //删除
        stringBuilder.delete(3,6);//abc
        //查找
        int pos1 = stringBuilder.lastIndexOf("ab",2);//0
        int pos2 = stringBuilder.lastIndexOf("bc",0);//1
        //设置
        stringBuilder.setCharAt(0,'A');
        //截取
        String s1 = stringBuilder.substring(0,1);
        System.out.println(s1);//A
    }

    //使用+拼接和使用StringBuffer或StringBuilder的比较效率
    public static void main3(String[] args) {
        long star1 = System.currentTimeMillis();//记录当前时间
        String s1 ="";//空字符串
        for (int i = 0; i < 1000; i++) {
            s1 += i;//拼接1 - 1000
        }
        long end1 = System.currentTimeMillis();//拼接末尾时间
        System.out.println(end1 - star1);//耗时 16毫秒

        long star2 = System.currentTimeMillis();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 1000; i++) {
            stringBuffer.append(i);//调用append方法
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - star2);//0

        long star3 = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            stringBuilder.append(i);
        }
        long end3 = System.currentTimeMillis();
        System.out.println(end3 -star3);//0

    }

    public static void main2(String[] args) {
        String[] s1 = new String[]{"ab","abc","abcd"};
       // s1[0].charAt(1) = "c";//不可修改
    }

    public static void main1(String[] args) {
        char[] chars = new char[]{'a','b','c'};//存放在堆区
        String s1 = new String(chars);//将数组转为字符串
        //调用intern方法，将字符串存放于常量池中
        String s2 = s1.intern();//intern方法的返回值是String类型
        //调用intern方法后s2与s1指向的是同一内存空间，s1 == s2
        System.out.println(s2);//输出'abc'
        System.out.println(s1 == s2);//ture
        //如果是比较内容可以调用equals方法
        System.out.println(s1.equalsIgnoreCase(s1));

        String s3 = "acd";
//       IntStream chars3 = s3.chars();
//        System.out.println(chars3);
    }
}